//
//  WBNavigationController.swift
//  weibo
//
//  Created by iobit on 2017/05/18.
//  Copyright © 2017 zhuxu. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //隐藏默认NavgationBar
        navigationBar.isHidden = true
    }
    
    //重写push方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        //main中实例化导航控制器时，childViewControllers.count为0
        if childViewControllers.count > 0 {
            //隐藏底部tabBar
            viewController.hidesBottomBarWhenPushed = true
            
            //判断控制器的类型
            if let vc = viewController as? WBBaseViewController {
                var title = "返回"
                //判断控制器级数
                if childViewControllers.count == 1 {
                    title = childViewControllers.first?.title ?? "返回"
                }
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBack: true)
            }
        }
        
        super.pushViewController(viewController, animated: true)
        
    }
    
    @objc private func popToParent() {
        popViewController(animated: true)
    }

}
